package com.crecheng.shop.controller;

import com.alibaba.fastjson.JSON;
import com.crecheng.shop.DAO.StarData;
import com.crecheng.shop.Manage.StarManage;
import com.crecheng.shop.util.MyMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StarController {

    @RequestMapping("/star/get")
    public @ResponseBody
    String getStar(@RequestParam(value = "uid",defaultValue = "-1")int uid,
                   @RequestParam(value = "id",defaultValue = "-1")int id){
        if(uid<1)
            return MyMessage.defaultError;
        else {
            if(id<1)
                return JSON.toJSONString(StarManage.getByUser(uid));
            else
                return JSON.toJSONString(StarManage.get("uid="+uid+" and id="+id));
        }
    }

    @RequestMapping("/star/add")
    public @ResponseBody
    String addStar(@RequestParam(value = "uid",defaultValue = "-1")int uid,
                   @RequestParam(value = "sid",defaultValue = "-1")int sid){
        if(uid<1||sid<1){
            return MyMessage.defaultError;
        }else {
            StarData data=new StarData();
            data.set(uid,sid);
            if(StarManage.add(data))
                return MyMessage.defaultSuccess;
            else
                return MyMessage.defaultSqlError;
        }
    }

    @RequestMapping("/star/del")
    public @ResponseBody
    String delStar(@RequestParam(value = "uid",defaultValue = "-1")int uid,
                   @RequestParam(value = "id",defaultValue = "-1")int id){
        if(uid<1)
            return MyMessage.defaultError;
        else {
            if(id<1)
                return MyMessage.toJson("delCount",StarManage.delByUser(uid));
            else
            {
                if(StarManage.del(id))
                    return MyMessage.defaultSuccess;
                else
                    return MyMessage.defaultSqlError;
            }
        }
    }
}
